package com.app.controller;

import com.app.service.CollectServer;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.entity.Code;
import com.common.entity.Collect;
import com.common.entity.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 王鑫
 * @create 2020 -11 -24 20:53
 */
@Api(tags = "收藏电桩模块")
@RestController
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    CollectServer collectServer;

    /**
     * 添加收藏
     *
     * @param userId
     * @param pileId
     * @return
     */
    @ApiOperation(value = "添加收藏")
    @PostMapping("/addCollect/{userId}/{pileId}")
    public ResponseResult addCollect(
            @ApiParam(name = "userId", value = "用户id", required = true) @PathVariable("userId") String userId,
            @ApiParam(name = "pileId", value = "电桩id", required = true) @PathVariable("pileId") String pileId) {
        try {
            collectServer.addCollect(userId, pileId);
            return ResponseResult.success(Code.REQUEST_SUCCESS).Data(null).Message("添加成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseResult.error(Code.REQUEST_ERROR).Message("添加失败");
        }
    }

    /**
     * 通过id删除收藏
     *
     * @param collectId
     * @return
     */
    @ApiOperation(value = "通过id删除收藏")
    @DeleteMapping("/deleteCollectById")
    public ResponseResult deleteCollectById(
            @ApiParam(name = "collectId", value = "收藏id", required = true) @RequestBody String collectId) {
        try {
            collectServer.deleteCollectById(collectId);
            return ResponseResult.success(Code.REQUEST_SUCCESS).Data(null).Message("删除成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseResult.error(Code.REQUEST_ERROR).Message("删除失败");
        }
    }

    /**
     * 通过id查询收藏
     *
     * @param collectId
     * @return
     */
    @ApiOperation(value = "通过id查询收藏")
    @PostMapping("/selectCollectById/{collectId}")
    public ResponseResult selectCollectById(
            @ApiParam(name = "collectId", value = "收藏id", required = true) @PathVariable("collectId") String collectId) {
        try {
            Collect collect = collectServer.selectCollectById(collectId);
            return ResponseResult.success(Code.REQUEST_SUCCESS).Data(collect);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseResult.error(Code.REQUEST_ERROR).Message("查询失败");
        }
    }

    /**
     * 查询所有收藏
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查询所有收藏")
    @PostMapping("/selectAllCollect")
    public ResponseResult selectAllCollect() {
        try {
            List<Collect> collects = collectServer.selectAllCollect();
            return ResponseResult.success(Code.REQUEST_SUCCESS).Data(collects);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseResult.error(Code.REQUEST_ERROR).Message("查询失败");
        }
    }

    /**
     * 分页查询所有收藏
     *
     * @return
     */
    @ApiOperation(value = "分页查询所有收藏")
    @PostMapping("/selectCollectByPage/{pageIndex}/{pageSize}")
    public ResponseResult selectCollectByPage(
            @ApiParam(name = "pageIndex", value = "当前页码", required = true) @PathVariable("pageIndex") int pageIndex,
            @ApiParam(name = "pageSize", value = "页大小", required = true) @PathVariable("pageSize") int pageSize) {
        try {
            //构造分页对象
            Page<Collect> page = new Page<>(pageIndex, pageSize);
            //开启分页
            collectServer.selectCollectByPage(page);
            //获取数据总条数
            long pageTotal = page.getTotal();
            //获取当前页信息
            List<Collect> records = page.getRecords();
            return ResponseResult.success(Code.REQUEST_SUCCESS).Data("allorderslimit", records);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseResult.error(Code.REQUEST_ERROR);
        }
    }

    /**
     * 多条件分页查询收藏
     *
     * @param collect
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "多条件分页查询收藏")
    @GetMapping("/selectCollectByPageAndCondition/{pageIndex}/{pageSize}")
    public ResponseResult selectCollectByPageAndCondition(
            @ApiParam(name = "collect", value = "收藏对象", required = true) Collect collect,
            @ApiParam(name = "pageIndex", value = "当前页码", required = true) @PathVariable("pageIndex") int pageIndex,
            @ApiParam(name = "pageSize", value = "页大小", required = true) @PathVariable("pageSize") int pageSize) {
        try {
            //构造分页对象
            Page<Collect> page = new Page<>(pageIndex, pageSize);
            //开启分页
            collectServer.selectCollectByPageAndCondition(page, collect);
            //获取数据总条数
            long pageTotal = page.getTotal();
            //获取当前页信息
            List<Collect> records = page.getRecords();
            return ResponseResult.success(Code.REQUEST_SUCCESS).Data("allorderslimitCondition", records);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseResult.error(Code.REQUEST_ERROR);
        }
    }

    @ApiOperation(value = "通过用户电话(账号)查询收藏记录")
    @PostMapping("/selectCollectByUserPhonee/{userPhone}")
    public ResponseResult selectCollectByUserPhone(
            @ApiParam(name = "userPhone", value = "用户电话", required = true) @PathVariable("userPhone") String userPhone) {
        try {
            List<Collect> collects = collectServer.selectCollectByUserPhone(userPhone);
            return ResponseResult.success(Code.REQUEST_SUCCESS).Data(collects);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseResult.error(Code.REQUEST_ERROR).Message("查询失败");
        }
    }

    @ApiOperation(value = "通过用户ID查询收藏记录")
    @PostMapping("/selectCollectByUserId/{userId}")
    public ResponseResult selectCollectByUserId(
            @ApiParam(name = "userId", value = "用户电话", required = true) @PathVariable("userId") String userId) {
        try {
            List<Collect> collects = collectServer.selectCollectByUserId(userId);
            return ResponseResult.success(Code.REQUEST_SUCCESS).Data(collects);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseResult.error(Code.REQUEST_ERROR).Message("查询失败");
        }
    }


}
